/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.hibernate;

import static org.openinsula.vulcano.orm.dao.hibernate.HibernateDao.QUERY_ALIAS;

import java.util.List;

import org.openinsula.vulcano.orm.dao.query.spec.AbstractGroupSpec;
import org.openinsula.vulcano.orm.dao.query.spec.AbstractSingleValuePropertySpec;
import org.openinsula.vulcano.orm.dao.query.spec.AndSpec;
import org.openinsula.vulcano.orm.dao.query.spec.BetweenSpec;
import org.openinsula.vulcano.orm.dao.query.spec.EqualsSpec;
import org.openinsula.vulcano.orm.dao.query.spec.GreaterOrEqualSpec;
import org.openinsula.vulcano.orm.dao.query.spec.GreaterSpec;
import org.openinsula.vulcano.orm.dao.query.spec.InSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LesserOrEqualSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LesserSpec;
import org.openinsula.vulcano.orm.dao.query.spec.LikeSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NotEqualsSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NotNullSpec;
import org.openinsula.vulcano.orm.dao.query.spec.NullSpec;
import org.openinsula.vulcano.orm.dao.query.spec.OrSpec;
import org.openinsula.vulcano.orm.dao.query.spec.Spec;
import org.openinsula.vulcano.orm.dao.query.spec.SpecVisitor;

/**
 * This {@link SpecVisitor} implementation translates a {@link Spec} into a HQL
 * <code>where</code> clause.
 * 
 * @author rebola
 * @author yanaga
 * @since 1.1
 * @see SpecVisitor
 */
public class HqlSpecVisitor implements SpecVisitor<String> {

	public String visit(AndSpec andSpec) {
		return groupSpecVisit(andSpec, " and ");
	}

	public String visit(OrSpec orSpec) {
		return groupSpecVisit(orSpec, " or ");
	}

	public String visit(EqualsSpec equalsSpec) {
		if (equalsSpec.getValue() != null) {
			return singleValuePropertyVisit(equalsSpec, "=");
		}
		else {
			return visit(new NullSpec(equalsSpec.getPropertyName()));
		}
	}

	public String visit(NotEqualsSpec notEqualsSpec) {
		if (notEqualsSpec.getValue() != null) {
			return singleValuePropertyVisit(notEqualsSpec, "<>");
		}
		else {
			return visit(new NotNullSpec(notEqualsSpec.getPropertyName()));
		}
	}

	public String visit(GreaterOrEqualSpec greaterOrEqualSpec) {
		return singleValuePropertyVisit(greaterOrEqualSpec, ">=");
	}

	public String visit(GreaterSpec greaterSpec) {
		return singleValuePropertyVisit(greaterSpec, ">");
	}

	public String visit(LesserOrEqualSpec lesserOrEqualSpec) {
		return singleValuePropertyVisit(lesserOrEqualSpec, "<=");
	}

	public String visit(LesserSpec lesserSpec) {
		return singleValuePropertyVisit(lesserSpec, "<");
	}

	public String visit(LikeSpec likeSpec) {
		String property = String.format("cast(%s.%s, string)", QUERY_ALIAS, likeSpec.getPropertyName());

		if (!likeSpec.isCaseSensitive()) {
			property = String.format("lower(%s)", property);
		}

		return property + " like ?";
	}

	public String visit(NotNullSpec notNullSpec) {
		return String.format("%s.%s is not null", QUERY_ALIAS, notNullSpec.getPropertyName());
	}

	public String visit(NullSpec nullSpec) {
		return String.format("%s.%s is null", QUERY_ALIAS, nullSpec.getPropertyName());
	}

	public String visit(BetweenSpec betweenSpec) {
		return String.format("%s.%s between ? and ?", QUERY_ALIAS, betweenSpec.getPropertyName());
	}

	public String visit(InSpec inSpec) {
		StringBuilder sb = new StringBuilder();

		sb.append(QUERY_ALIAS);
		sb.append('.');
		sb.append(inSpec.getPropertyName());
		sb.append(" in (?");

		for (int i = 1, j = inSpec.getValues().size(); i < j; i++) {
			sb.append(",?");
		}
		sb.append(")");

		return sb.toString();
	}

	private String groupSpecVisit(AbstractGroupSpec spec, String clause) {
		StringBuilder sb = new StringBuilder();
		sb.append("(");

		List<Spec> expressions = spec.getExpressions();
		sb.append(expressions.get(0).accept(this));

		for (int i = 1; i < expressions.size(); i++) {
			sb.append(clause);
			sb.append(expressions.get(i).accept(this));
		}

		sb.append(")");

		return sb.toString();
	}

	private String singleValuePropertyVisit(AbstractSingleValuePropertySpec spec, String operation) {
		return String.format("%s.%s %s ?", QUERY_ALIAS, spec.getPropertyName(), operation);
	}
}
